package cn.green.core.scanner;

import cn.green.core.annos.table.QTable;
import cn.green.core.adapt.models.table.OrmTable;
import cn.green.core.scanner.annos.EnableQOrm;
import cn.green.core.scanner.store.OrmTableStore;
import cn.green.utils.ColumnUtils;
import cn.green.utils.StringUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.util.ClassUtils;

import java.util.*;

/**
 * @author wang.yishu
 * @version 1.0
 * @className QOrmTableScanner
 * @description 表实体扫描器
 * @date 2025/5/9 15:57
 */
public class QOrmTableScanner  implements ImportSelector {

    public static final String BASE_PACKAGE = "cn.green";

    public static Set<String> tableClassNames = new HashSet<>();

    public static boolean canInitQOrmTable = false;

    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        List<String> allTableClassPath = new ArrayList<>();
        ClassPathScanningCandidateComponentProvider provider =
                new ClassPathScanningCandidateComponentProvider(false);
        provider.addIncludeFilter(new AnnotationTypeFilter(QTable.class));
        String[] packages = getPackageToScan(annotationMetadata);
        for (String aPackage : packages) {
            Set<BeanDefinition> beanDefinitions = provider.findCandidateComponents(aPackage);
            beanDefinitions.forEach(beanDefinition -> allTableClassPath.add(beanDefinition.getBeanClassName()));
        }
        for (String tableClassPath : allTableClassPath) {
            canInitQOrmTable = true;
            try {
                Class<?> tableType = Class.forName(tableClassPath);
                QTable qTable = tableType.getDeclaredAnnotation(QTable.class);
                tableClassNames.add(tableClassPath);
                //建造表信息和字段信息
                System.out.println("OrmTable! -> class: " + tableType.getName() +
                        " | table: " +
                        (StringUtils.isEmpty(qTable.source()) ? "masterSource" : qTable.source()) + "." +
                        (StringUtils.isEmpty(qTable.scheme()) ? "masterScheme" : qTable.scheme()) + "." +
                        qTable.name() +
                        " | version: " + qTable.version());

            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        OrmTableStore.hasInit();
        return allTableClassPath.toArray(new String[0]);
    }

    private String[] getPackageToScan(AnnotationMetadata metadata) {
        //通过自定义注解获取自定义包路径
        AnnotationAttributes attributes = AnnotationAttributes.fromMap(
                metadata.getAnnotationAttributes(EnableQOrm.class.getName()));
        if (attributes == null) {
            return new String[]{BASE_PACKAGE};
        }
        String[] basePackages = attributes.getStringArray("basePackages");
        if (basePackages.length != 0) {
            Set<String> rtnSet = new HashSet<>(Arrays.asList(basePackages));
            rtnSet.add(BASE_PACKAGE);
            return rtnSet.toArray(new String[1]);
        }
        return new String[]{ClassUtils.getPackageName(metadata.getClassName())};
    }
}
